package logic

import (
	"context"
	"fmt"
	"wechats/models/mysql"

	"wechats/wechatrpc/internal/svc"
	"wechats/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ImproveLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewImproveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImproveLogic {
	return &ImproveLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ImproveLogic) Improve(in *wechatrpc.ImproveRequest) (*wechatrpc.ImproveResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在")
	}

	user = mysql.User{
		HeadImage: in.HeadImage,
		Name:      in.Name,
		Address:   in.Address,
	}
	mysql.DB.Debug().Updates(&user)

	return &wechatrpc.ImproveResponse{
		UserId: int64(user.ID),
	}, nil
}
